package com.shareware.common.utils

import com.shareware.common.model.TimeInfo
import com.shareware.common.model.TimeInt
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale
import java.util.TimeZone

/**
 * desc: 功能描述
 * email: mobiledeveloper@qq.com
 */

/**
 * 格式化时间
 */
fun Long.toDateTime(pattern: String): String {
    return SimpleDateFormat(pattern, Locale.CHINA).format(Date(this))
}

/**
 * 格式化时间
 */
fun Date.toDateTime(pattern: String): String {
    return SimpleDateFormat(pattern, Locale.CHINA).format(this)
}

fun Long.toGMT(): String {
    val sdf = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US)
    sdf.timeZone = TimeZone.getTimeZone("GMT")
    return sdf.format(Date(this))
}

/**
 * convert GMT string to long
 *
 * @return long
 */
fun String.gmtToLong(): Long {
    if (this.isEmpty()) {
        return System.currentTimeMillis()
    }
    val sdf = SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US)
    sdf.timeZone = TimeZone.getTimeZone("GMT")
    return try {
        sdf.parse(this)?.time ?: 0
    } catch (ignore: Exception) {
        0
    }
}

/**
 * @return 返回当前系统时间yyyy-MM-dd HH:mm:ss
 */
fun getCurrentTime(): String {
    return SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA).format(Date())
}

/**
 * @return 返回当前系统时间 yyyy-MM-dd 天
 */
fun getDayTime(): String {
    return SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(Date())
}

/**
 * 获取明天时间戳
 */
fun getTomorrowDateTime(): Long {
    val calendar = Calendar.getInstance()
    calendar.add(Calendar.DAY_OF_MONTH, 1)
    return calendar.time.time
}

/**
 * 获取当前时间为每年第几周
 *
 * @return 第几周
 */
fun Date.getWeekOfYear(): Int {
    val c = Calendar.getInstance()
    c.firstDayOfWeek = Calendar.MONDAY
    c.time = this
    var week = c.get(Calendar.WEEK_OF_YEAR) - 1
    week = if (week == 0) 52 else week
    return if (week > 0) week else 1
}

/**
 * long days = millisecond / (1000 * 60 * 60 * 24);
 * long hours = (millisecond % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
 * @return 00：00
 */
fun Long.toMinuteAndSecond(): String {
    val minutes = this / (1000 * 60)
    val seconds = this % (1000 * 60) / 1000
    val strMinute = if (minutes < 10) "0$minutes" else "" + minutes//分钟
    val strSecond = if (seconds < 10) "0$seconds" else "" + seconds//秒
    return "$strMinute:$strSecond"
}

fun Long.toHourMinute(): String {
    val hour = this / (1000 * 60 * 60)
    val minutes = this % (1000 * 60 * 60) / (1000 * 60)
    val strHour = if (hour < 10) "0$hour" else "" + hour//小时
    val strMinute = if (minutes < 10) "0$minutes" else "" + minutes//分钟
    return "$strHour:$strMinute"
}

fun Long.toDayHourMinuteSecond(): TimeInfo {
    val day = this / (1000 * 60 * 60 * 24)
    var remain = this % (1000 * 60 * 60 * 24)
    val hour = remain / (1000 * 60 * 60)
    remain %= (1000 * 60 * 60)
    val minutes = remain / (1000 * 60)
    remain %= (1000 * 60)
    val seconds = remain / 1000
    val strHour = if (hour < 10) "0$hour" else "$hour"//小时
    val strMinute = if (minutes < 10) "0$minutes" else "$minutes"//分钟
    val strSecond = if (seconds < 10) "0$seconds" else "$seconds"//秒
    return TimeInfo("$day", strHour, strMinute, strSecond)
}

fun Long.toDayHourMinuteSecond2Int(): TimeInt {
    val day = this / (1000 * 60 * 60 * 24)
    var remain = this % (1000 * 60 * 60 * 24)
    val hour = remain / (1000 * 60 * 60)
    remain %= (1000 * 60 * 60)
    val minutes = remain / (1000 * 60)
    remain %= (1000 * 60)
    val seconds = remain / 1000
    return TimeInt(day.toInt(), hour.toInt(), minutes.toInt(), seconds.toInt())
}

fun getGmtTimeZone(): String {
    val rawOffset = TimeZone.getDefault().rawOffset
    val timeZonePattern: String = if (rawOffset < 0) {
        "z-HH:mm"
    } else {
        "z+HH:mm"
    }
    val sdf = SimpleDateFormat(timeZonePattern, Locale.US)
    sdf.timeZone = TimeZone.getTimeZone("GMT")
    return try {
        sdf.format(Date(rawOffset.toLong()))
    } catch (ignore: Exception) {
        ""
    }
}